﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace WeiSha.Core.Images
{
	// Token: 0x02000040 RID: 64
	public class FileTo
	{
		// Token: 0x0600029B RID: 667 RVA: 0x00011C24 File Offset: 0x0000FE24
		public static string Thumbnail(string sourceFile, string thFile, int width, int height, int restrainObj)
		{
			bool flag = !File.Exists(sourceFile);
			string text;
			if (flag)
			{
				text = sourceFile;
			}
			else
			{
				using (Image image = Image.FromFile(sourceFile, true))
				{
					Image image2 = ImageTo.Thumbnail(image, width, height, restrainObj);
					ImageFormat imageFormat = FileTo.GetImageFormat(thFile);
					image2.Save(thFile, imageFormat);
					image.Dispose();
					image2.Dispose();
				}
				text = thFile;
			}
			return text;
		}

		// Token: 0x0600029C RID: 668 RVA: 0x00011C9C File Offset: 0x0000FE9C
		public static string OverlayImage(string sourceFile, string overFile, string overType, int opacity)
		{
			bool flag = !File.Exists(sourceFile);
			string text;
			if (flag)
			{
				text = sourceFile;
			}
			else
			{
				bool flag2 = !File.Exists(overFile);
				if (flag2)
				{
					text = sourceFile;
				}
				else
				{
					Image image = Image.FromFile(sourceFile);
					Image image2 = Image.FromFile(overFile);
					image = ImageTo.Overlay(image, image2, overType, opacity);
					string text2 = sourceFile.Substring(0, sourceFile.LastIndexOf(".")) + "_new" + sourceFile.Substring(sourceFile.LastIndexOf("."));
					ImageFormat imageFormat = FileTo.GetImageFormat(text2);
					image.Save(text2, imageFormat);
					image.Dispose();
					image2.Dispose();
					File.Delete(sourceFile);
					File.Move(text2, sourceFile);
					text = sourceFile;
				}
			}
			return text;
		}

		// Token: 0x0600029D RID: 669 RVA: 0x00011D50 File Offset: 0x0000FF50
		public static string OverlayText(string sourceFile, string text, string overType, int opacity)
		{
			bool flag = !File.Exists(sourceFile);
			string text2;
			if (flag)
			{
				text2 = sourceFile;
			}
			else
			{
				using (Image image = Image.FromFile(sourceFile))
				{
					Image image2 = ImageTo.Overlay(image, text, overType, opacity);
					string text3 = sourceFile.Substring(0, sourceFile.LastIndexOf(".")) + "_new" + sourceFile.Substring(sourceFile.LastIndexOf("."));
					ImageFormat imageFormat = FileTo.GetImageFormat(text3);
					image2.Save(text3, imageFormat);
					image.Dispose();
					image2.Dispose();
					File.Delete(sourceFile);
					File.Move(text3, sourceFile);
				}
				text2 = sourceFile;
			}
			return text2;
		}

		// Token: 0x0600029E RID: 670 RVA: 0x00011E08 File Offset: 0x00010008
		public static string Zoom(string sourceFile, int width, int height)
		{
			bool flag = !File.Exists(sourceFile);
			string text;
			if (flag)
			{
				text = sourceFile;
			}
			else
			{
				bool flag2 = width < 1 || height < 1;
				if (flag2)
				{
					text = sourceFile;
				}
				else
				{
					using (Image image = Image.FromFile(sourceFile))
					{
						Image image2 = ImageTo.Zoom(image, width, height);
						string text2 = sourceFile.Substring(0, sourceFile.LastIndexOf(".")) + "_new" + sourceFile.Substring(sourceFile.LastIndexOf("."));
						ImageFormat imageFormat = FileTo.GetImageFormat(text2);
						image2.Save(text2, imageFormat);
						image.Dispose();
						image2.Dispose();
						File.Delete(sourceFile);
						File.Move(text2, sourceFile);
					}
					text = sourceFile;
				}
			}
			return text;
		}

		// Token: 0x0600029F RID: 671 RVA: 0x00011ED8 File Offset: 0x000100D8
		public static string Zoom(string sourceFile, int width, int height, bool isDeformation)
		{
			string text;
			if (isDeformation)
			{
				text = FileTo.Zoom(sourceFile, width, height);
			}
			else
			{
				bool flag = !File.Exists(sourceFile);
				if (flag)
				{
					text = sourceFile;
				}
				else
				{
					bool flag2 = width < 1 || height < 1;
					if (flag2)
					{
						text = sourceFile;
					}
					else
					{
						using (Image image = Image.FromFile(sourceFile))
						{
							Image image2 = ImageTo.Zoom(image, width, height, isDeformation);
							string text2 = sourceFile.Substring(0, sourceFile.LastIndexOf(".")) + "_new" + sourceFile.Substring(sourceFile.LastIndexOf("."));
							image2.Save(text2, ImageFormat.Jpeg);
							image.Dispose();
							image2.Dispose();
							File.Delete(sourceFile);
							File.Move(text2, sourceFile);
						}
						text = sourceFile;
					}
				}
			}
			return text;
		}

		// Token: 0x060002A0 RID: 672 RVA: 0x00011FBC File Offset: 0x000101BC
		public static Image ToImage(string sourceFile)
		{
			bool flag = !File.Exists(sourceFile);
			Image image;
			if (flag)
			{
				image = null;
			}
			else
			{
				Image image2 = Image.FromFile(sourceFile);
				image = image2;
			}
			return image;
		}

		// Token: 0x060002A1 RID: 673 RVA: 0x00011FE8 File Offset: 0x000101E8
		public static string ToBase64(string imgFile)
		{
			bool flag = !File.Exists(imgFile);
			string text;
			if (flag)
			{
				text = null;
			}
			else
			{
				string text2 = string.Empty;
				using (Bitmap bitmap = new Bitmap(imgFile))
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						ImageFormat imageFormat = FileTo.GetImageFormat(imgFile);
						bitmap.Save(memoryStream, imageFormat);
						byte[] array = new byte[memoryStream.Length];
						memoryStream.Position = 0L;
						memoryStream.Read(array, 0, (int)memoryStream.Length);
						memoryStream.Close();
						text2 = Convert.ToBase64String(array);
					}
					bitmap.Dispose();
				}
				text = text2;
			}
			return text;
		}

		// Token: 0x060002A2 RID: 674 RVA: 0x000120B8 File Offset: 0x000102B8
		public static string ToBase64Html(string imgFile)
		{
			bool flag = !File.Exists(imgFile);
			string text;
			if (flag)
			{
				text = null;
			}
			else
			{
				string text2 = string.Empty;
				using (Bitmap bitmap = new Bitmap(imgFile))
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						ImageFormat imageFormat = FileTo.GetImageFormat(imgFile);
						bitmap.Save(memoryStream, imageFormat);
						byte[] array = new byte[memoryStream.Length];
						memoryStream.Position = 0L;
						memoryStream.Read(array, 0, (int)memoryStream.Length);
						memoryStream.Close();
						text2 = Convert.ToBase64String(array);
					}
					bitmap.Dispose();
				}
				text = "data:image/jpeg;base64," + text2;
			}
			return text;
		}

		// Token: 0x060002A3 RID: 675 RVA: 0x00012190 File Offset: 0x00010390
		public static ImageFormat GetImageFormat(string fileName)
		{
			string text = Path.GetExtension(fileName).Substring(1).ToLower();
			string text2 = text;
			string text3 = text2;
			ImageFormat imageFormat;
			if (!(text3 == "bmp"))
			{
				if (!(text3 == "gif"))
				{
					if (!(text3 == "ico") && !(text3 == "icon"))
					{
						imageFormat = ImageFormat.Jpeg;
					}
					else
					{
						imageFormat = ImageFormat.Icon;
					}
				}
				else
				{
					imageFormat = ImageFormat.Gif;
				}
			}
			else
			{
				imageFormat = ImageFormat.Bmp;
			}
			return imageFormat;
		}
	}
}
